$(function() {
	platformId = "";// 当前页面所使用的平台id
	getPlatformId();// 
	//数据初始值
	getTCRLocalStorageInit();
	//监听数据变化，存储本地数据
	addListenerEventFun();
	addNumberKeyboard();
});

function getDate(inputs,selects){
	var str='{';
	for(var i=0;i<inputs.length;i++){//获取Input的键值
    	var input = $(inputs[i]);
    	var key =input.attr("name");
   		var value =input.val();
    	str += key+':"'+value+'",';
    }
    for(var i=0;i<selects.length;i++){//获取select的键值
    	var select = $(selects[i]);
    	var key =select.attr("name");
   		var value =select.find("option:selected").attr("signalValue"); 
   		if(selects.length-1==i){
			str += key+':"'+value+'"}';
    	}else{
    		str += key+':"'+value+'",';
    	}
    }
    return str;
}
function ajaxSendTCRInfo(str,sendSignal) {//外围设备信息发送功能
	addLoading();
	$.ajax({
		"url" : "../../../tcr/ajaxSendTCRInfo",
		"type" : "post",
		"data" : {
			"sendSignal" : sendSignal,
			"platformId" : platformId,
			"str" : str
		},
		"success" : function(result) {
			removeLoading();
			if (result.flag == 0) {
				console.log("发送成功");
				book('发送成功');
			} else {
				var content=result.errorInfo;
				book(content);
			}
		},
		"error" : function() {
			removeLoading();
			var content="手工测试连接错误！";
			book(content);
		}
	});
}

function getPlatformId() {// 从测试序列界面的链接路径中截取ID
	var url = location.search; // 获取url中"?"符后的字串
	path = location.host;
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		platformId = strs[3].split("=")[1];
	}
}
tests=function(moudle){ // tcr页面数据发送功能
 	var inputs=$("#buttom input");//拿到所有的输入框
 	var selects=$("#buttom select");//拿到所有的下拉框
	if(moudle=="start"){//点击的是开始和终止按钮
		console.log("开始测试");
		var str=getDate(inputs,selects);
		ajaxSendTCRInfo(str,1);
	}
	else if(moudle=="stop"){
		console.log("停止测试");
		var str=getDate(inputs,selects);
		ajaxSendTCRInfo(str,3);
	}
	else if(moudle=="updated"){
		console.log("更新数据");
		var str=getDate(inputs,selects);
		console.log(str);
		ajaxSendTCRInfo(str,2);
	}	
 };
//监听数据变化，存储本地数据
 function addListenerEventFun(){
 	//监听数据变化，存储本地数据
 	$("input").on('input',function(e)  
 	{  
 		switch (e.target.name){
 			case 'DelayTime':
 				localStorage.setItem('TCRDelayTime', e.target.value);
 				break;
 			default:
 				break;
 		}
 	}); 
 	
 	//监听数据变化，存储本地数据
 	$("select").on('change',function(e){
 		switch (e.target.name){
 			case 'MainOrSlave':
 				localStorage.setItem('TCRMainOrSlave', e.target.selectedIndex);
 				break;
 			case 'Producer':
 				localStorage.setItem('TCRProducer', e.target.selectedIndex);
 				break;
 			case 'SeDeng':
 				localStorage.setItem('TCRSeDeng', e.target.selectedIndex);
 				break;
 			case 'UpOrDown':
 				localStorage.setItem('TCRUpOrDown', e.target.selectedIndex);
 				break;
 			case 'Frequency':
 				localStorage.setItem('TCRFrequency', e.target.selectedIndex);
 				break;
 			default:
 				break;
 		}
 	});
 }
//读取存储数据
 function getTCRLocalStorageInit(){
 	if(localStorage.getItem('TCRDelayTime')){
 		$("input[name='DelayTime']").val(localStorage.getItem('TCRDelayTime'));
 	}
 	
 	if(localStorage.getItem('TCRMainOrSlave')){
 		$("select[name = 'MainOrSlave']").get(0).selectedIndex = localStorage.getItem('TCRMainOrSlave');
 	}
 	if(localStorage.getItem('TCRProducer')){
 		$("select[name = 'Producer']").get(0).selectedIndex = localStorage.getItem('TCRProducer');
 	}
 	if(localStorage.getItem('TCRSeDeng')){
 		$("select[name = 'SeDeng']").get(0).selectedIndex = localStorage.getItem('TCRSeDeng');
 	}
 	if(localStorage.getItem('TCRUpOrDown')){
 		$("select[name = 'UpOrDown']").get(0).selectedIndex = localStorage.getItem('TCRUpOrDown');
 	}
 	if(localStorage.getItem('TCRFrequency')){
 		$("select[name = 'Frequency']").get(0).selectedIndex = localStorage.getItem('TCRFrequency');
 	}
 }
 
//添加数字软键盘
 function addNumberKeyboard(){
	 if($(".numkeyboard").length>0){
			$(".numkeyboard").numkeyboard({
				keyboardRadix: 600, //键盘大小基数
				mainbackground: '#313439', //主背景色
				menubackground: '#313439', //头背景色
				exitbackground: '#4376A0', //关闭按钮背景色
				buttonbackground: 'transparent', //键盘背景色
				clickeve: true, //是否绑定元素click事件
			});
		}
 }